//引入axios模块
import store from '@/store';
import axios from 'axios';
import 'nprogress/nprogress.css'
import NProress from 'nprogress';
import { getUserTempId } from '@/utils/getUserTempId';
//创建实例
const ajax = axios.create({
  baseURL: '/api',
  timeout:5000,//设置超时时间
});
//设置请求拦截
ajax.interceptors.request.use(config => {
  NProress.start();//启用进度条
  config.headers.userTempId = getUserTempId()
  const token = store.state.user.userInfo.token;
  // console.log('tokennnn',token)
  if (token) {
    config.headers.token = token;
  }
  return config

});

//设置响应拦截器
ajax.interceptors.response.use(
  response => {
    NProress.done()//结束进度条
    return response.data
  },
  error => {
    NProress.done()//结束进度条
    return Promise.reject(new Error(error.message))
  }
);

//导出对象
export default ajax
